#include <bits/stdc++.h>
using namespace std;
int dp[200005][2];
int a[200005];
vector<int> v;
map<int,int> f;
int look_for(int g)
{
    for(int i=0;i<v.size();i++)
    {
        if(v[i]==g)
        {
            return i;
        }
    }
}
int work(int r,int t)
{
    int l=look_for(v[r]),g=0;
    map<int,int> f1;
    for(int i=l+1;i<r;i++)
    {
        f1[v[i]]++;
        if(f1[v[i]]>=2)g+=v[i];
    }
    return g;
}
int main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(a,0,sizeof(a));
        memset(dp,0,sizeof(dp));
        while(v.size())v.erase(v.begin());
        long long ans=0;
        int n;
        scanf("%d",&n);
        int sum=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            f[a[i]]=0;
        }
        for(int i=1;i<=n;i++)
        {
            f[a[i]]++;
        }
        for(int i=1;i<=n;i++)
        {
            if(f[a[i]]>1)
            {
                v.push_back(a[i]);
            }
        }
        if(!v.size())
        {
            cout<<0<<endl;
            continue;
        }
        dp[0][0]=0;
        dp[0][1]=0;
        for(int i=1;i<v.size();i++)
        {
            if(look_for(v[i])==i)
            {
                dp[i][1]=max(dp[i-1][1],dp[i-1][0]);
                dp[i][0]=max(dp[i-1][1],dp[i-1][0]);
            }
            else
            {
                dp[i][1]=max(max(dp[i-1][1],dp[i-1][0]),dp[i-1][0]-work(i,0)+v[i]);
                dp[i][0]=max(max(dp[i-1][1],dp[i-1][0]),dp[i-1][1]-work(i,1)+v[i]);
            }
        }
        dp[v.size()][0]=max(dp[v.size()-1][0],dp[v.size()-1][1]);
        cout<<dp[v.size()][0]<<endl;
    }
    return 0;
}
/*

*/
